#[derive(Clone, Copy, Debug)]
pub struct Node {
    pub x: f64,
    pub y: f64,
}

impl Node {
    pub fn new(x: f64, y: f64) -> Node {
        Node { x, y }
    }
    pub fn rand(st: Node, ed: Node) -> Node {
        use rand::Rng;
        let mut rng = rand::thread_rng();
        Node {
            x: rng.gen_range(st.x..ed.x),
            y: rng.gen_range(st.y..ed.y),
        }
    }
}

pub fn rand_node(st: Node, ed: Node, n: u32) -> Vec<Node> {
    (0..n).map(|_| Node::rand(st, ed)).collect()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn rand_check() {
        let node = Node::rand(Node::new(0., 9.), Node::new(10., 19.));
        assert!(0. <= node.x && node.x <= 10. && 9. <= node.y && node.y <= 19.);
    }

    #[test]
    fn rand_list_check() {
        let n = 10;
        let list = rand_node(Node::new(0., 9.), Node::new(10., 19.), n);
        for node in list {
            assert!(0. <= node.x && node.x <= 10. && 9. <= node.y && node.y <= 19.);
        }
    }
}
